import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getToken, setToken, removeToken } from '@/utils/auth'

export const useUserStore = defineStore('user', () => {
  const token = ref(getToken())
  const userInfo = ref({
    username: '',
    nickname: '',
    role: '',
    avatar: '',
    permissions: []
  })

  const login = async (username, password) => {
    if (username === 'admin' && password === '123456') {
      const token = 'mock-token'
      setToken(token)
      return true
    }
    return false
  }

  const getUserInfo = async () => {
    userInfo.value = {
      username: 'admin',
      nickname: '管理员',
      role: '超级管理员',
      avatar: '',
      permissions: ['*']
    }
  }

  const logout = () => {
    removeToken()
    userInfo.value = {
      username: '',
      nickname: '',
      role: '',
      avatar: '',
      permissions: []
    }
  }

  return {
    token,
    userInfo,
    login,
    getUserInfo,
    logout
  }
}) 